package com.example.new5;


import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;


import com.example.new5.entity.Book;
import com.example.new5.entity.School;
import com.example.new5.entity.Student;
import com.example.new5.entity.Data;
import com.example.new5.utils.JsonUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private TextView mBook,mOlympic,mHot;
    private Fragment mFragment;
    private Student mStudent;
    private Student mStudent1;

//    private Student mStudent;
//    private Student mStudent1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

    }

    public void init() {
        objToJson();
//        jsonToObj();
//        getJson("peoples.json");
//         objToJson2();
        //jsonToObj2();
        //jsonToObj3();
        //jsonToList();
        //testJsonArray();
        //getValueByKey();
        //testJsonParser();
    }

    private String objToJson() {
        mStudent = new Student();
        mStudent.setStudent_code("22");
        mStudent.setStudent_id(1);
        mStudent.setStudent_name("刘老八");
        mStudent.setUsername("abcdef");
        mStudent.setPassword("123456");
        mStudent.setClass_name("计算机10班");
        Gson gson = new Gson();
        String jsonStr = gson.toJson(mStudent);
        Log.d("asdf", "objToJson : " + jsonStr);
        return jsonStr;
    }

    private void jsonToObj() {
        String jsonStr = objToJson();
        Gson gson = new Gson();
        mStudent1 = gson.fromJson(jsonStr, Student.class);
        Log.d("asdf", "jsonToObj = " + mStudent1.toString());
    }

    private void objToJson2() {
        Student student = new Student();
        student.setStudent_code("22");
        student.setStudent_id(1);
        student.setStudent_name("刘老八");
        student.setUsername("abcdef");
        student.setPassword("123456");
        student.setClass_name("计算机10班");

        Student student2 = new Student();
        student2.setStudent_code("22");
        student2.setStudent_id(1);
        student2.setStudent_name("刘老八");
        student2.setUsername("abcdef");
        student2.setPassword("123456");
        student2.setClass_name("计算机10班");
        List<Student> students = new ArrayList<>();
        students.add(student);
        students.add(student2);

        School school = new School();
        school.setName("吉林农业科技学院");
        school.setAddress("吉林市");
        school.setStudents(students);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(school);
        Log.d("asdf", "school = " + jsonStr);
    }
//
//    private void jsonToObj2() {
//        String jsonStr = getJson("school.json");
//        Gson gson = new Gson();
//        School school = gson.fromJson(jsonStr, School.class);
//        Log.d("asdf", "jsonToObj = " + school.toString());
//    }
//
//    private void jsonToObj3() {
//        String jsonStr = getJson("student_list.json");
//        Gson gson = new Gson();
//        Data student_list = gson.fromJson(jsonStr, Data.class);
//        Log.d("asdf", "jsonToObj = " + student_list.getData().getGrade2021());
//    }
//
//    private void jsonToList() {
//        Gson gson = new Gson();
//        String jsonStr = getJson("peoples.json");
//        //Type type = new TypeToken<List<People>>() {}.getType();
//        //List<People> list = gson.fromJson(jsonStr, type);
//
//        List<People> list = JsonUtils.jsonToList(jsonStr, People.class);
//
//        for(People people : list) {
//            Log.d("asdf", "people = " + people.toString());
//        }
//    }
//
//    private void testJsonArray() {
//        JsonArray jsonArray = new JsonArray();
//        jsonArray.add("老张");
//        jsonArray.add("老李");
//        jsonArray.add("老王");
//        String jsonStr = jsonArray.toString();
//        Log.d("asdf", "json array = " + jsonStr);
//    }
//
//    private void getValueByKey() {
//        String jsonStr = getJson("news.json");
//        String value = JsonUtils.getKeyRecursively(jsonStr, "thumbnail_pic_s");
//        Log.d("asdf", "getValueByKey value ==== " + value);
//    }
//
//    private void testJsonParser() {
//        String jsonStr = getJson("student_list.json");
//        JsonObject jo = JsonParser.parseString(jsonStr).getAsJsonObject();
//        JsonObject data = jo.get("data").getAsJsonObject();
//        JsonArray array = data.get("grade2021").getAsJsonArray();
//        for (JsonElement jsonElement : array) {
//            String name = jsonElement.getAsJsonObject().get("student_name").getAsString();
//            Log.d("asdf", " ==name== " + name);
//        }
//    }
//
    private String getJson(String name) {
        String json = JsonUtils.getJsonFromAssets(this, name);
        Log.d("asdf", "getJson = " + json);
        return json;
    }
}